package com.lyf.juc.learn.sggadvance.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author LYF
 * @date 2022/7/1
 */
public class CompletableApiTests {

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        CompletableFuture<Integer> task1 = CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 模拟计算为1
            return 1;
        });

        CompletableFuture<Integer> task2 = CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 1;
        }).thenApply(f-> f+task1.join()).whenComplete(((integer, throwable) -> {
            if(throwable==null)
                System.out.println(integer);
            System.out.println(System.currentTimeMillis()-start);

        })).exceptionally(e->{
            System.out.println(e.getCause());
            return null;
        });
        long end = System.currentTimeMillis();
         // 防止 默认 forkJoin--
        TimeUnit.SECONDS.sleep(3);
        System.out.println("耗时::"+(end-start)+":::");
    }

}
